﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace VoltcraftControlInterface
{
    class PolynomialIntegrator
    {
        public double DeltaQ;
        public double Pow;
        public PolynomialIntegrator()
        {
            DeltaQ = Pow = 0;
        }
        public void ParsePlusMinus(string input, ref Double outvalue)
        {
            if (input.Substring(0, 1) == "+") outvalue = Convert.ToDouble(input.Substring(1));
            else if (input.Substring(0, 1) == "-") outvalue = Convert.ToDouble(input.Substring(1)) * -1;
            else
                outvalue = Convert.ToDouble(input);
        }
        public void IntegrateAndCalculate(string expression, Double T2, Double T1, Double t2, Double t1)
        {
            DeltaQ = 0;
            Pow = 0;
            /* EXAMPLE EXPRESSION Ax^n+Bx^(n-1)+...+C */
            if (expression != "")
            {
                Double[] coefficients = new Double[50];
                Int32[] powers = new Int32[50];
                char[] plusminus = { '+', '-' };
                int numx = expression.Length - expression.Replace("x", "").Length;
                if (numx != 0)
                {
                    bool alonecoeff;
                    if ((expression.LastIndexOf('+') > expression.LastIndexOf('x')) || (expression.LastIndexOf('-') > expression.LastIndexOf('x'))) alonecoeff = true;
                    else alonecoeff = false;
                    int[] xoffsets = new int[50];
                    int currindex = 0;
                    for (int i = 0; i < numx; i++)
                    {
                        currindex = expression.IndexOf("x", currindex + 1);
                        xoffsets[i] = currindex;
                    }
                    int lastoffset = 0;
                    for (int i = 0; i < numx; i++)
                    {
                        if (expression.Substring(lastoffset, xoffsets[i] - lastoffset) != "")
                        {
                            ParsePlusMinus(expression.Substring(lastoffset, xoffsets[i] - lastoffset), ref coefficients[i]);
                            //System.Windows.Forms.MessageBox.Show("dovrebbe essere il coeff di x:" + coefficients[i].ToString());
                        }
                        else coefficients[i] = 0;
                        lastoffset = xoffsets[i];
                        if (i == (numx - 1) && !alonecoeff)
                            powers[i] = Convert.ToInt32(expression.Substring(lastoffset + 2));
                        else powers[i] = Convert.ToInt32(expression.Substring(lastoffset + 2, expression.IndexOfAny(plusminus, lastoffset+2) - (lastoffset + 2)));
                        //System.Windows.Forms.MessageBox.Show("dovrebbe essere la potenza di x:" + powers[i].ToString());
                        lastoffset += 2 + powers[i].ToString().Length;
                    }

                    //INTEGRATE!!!
                    for (int i = 0; i < numx; i++)
                    {
                        DeltaQ += (coefficients[i] / (powers[i] + 1)) * (Math.Pow(T2, powers[i] + 1) - Math.Pow(T1, powers[i] + 1));
                    }
                    if (alonecoeff)
                    {
                        Double lastcoeff = Convert.ToDouble(expression.Substring(xoffsets[numx - 1] + 2 + powers[numx - 1].ToString().Length));
                        DeltaQ += lastcoeff * (T2 - T1);
                        //System.Windows.Forms.MessageBox.Show("ultimo coeff:" + lastcoeff.ToString());
                    }
                    Pow = DeltaQ / (t2 - t1);
                }
                else
                {
                    DeltaQ = Convert.ToDouble(expression) * (T2 - T1);
                    Pow = DeltaQ / (t2 - t1);
                }
            }
            else
            {
                throw new System.ArgumentException("No expression entered!");
            }
        }
    }
}
